package com.youfan.control;

import com.alibaba.fastjson.JSONObject;
import com.youfan.entity.EsPage;
import com.youfan.utils.ElasticsearchUtil2;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("search2")
public class SearchOut2Control {

    @RequestMapping(value = "createIndex")
    public boolean createIndex(@RequestParam  String index) {

        return ElasticsearchUtil2.createIndex(index);
    }

    @RequestMapping(value = "deleteIndex")
    public boolean deleteIndex(@RequestParam  String index) {

        return ElasticsearchUtil2.deleteIndex(index);
    }


    @RequestMapping(value = "addData",method = RequestMethod.POST)
    public String addData(@RequestBody JSONObject jsonObject) {
        String index = jsonObject.getString("index");
        String type = jsonObject.getString("type");
        String id = jsonObject.getString("id");
        return ElasticsearchUtil2.addData(jsonObject,index,type,id);
    }

    @RequestMapping(value = "deleteDataById")
    public void deleteDataById(@RequestBody JSONObject jsonObject) {
        String index = jsonObject.getString("index");
        String type = jsonObject.getString("type");
        String id = jsonObject.getString("id");
        ElasticsearchUtil2.deleteDataById(index,type,id);
    }

    @RequestMapping(value = "updateDataById",method = RequestMethod.POST)
    public  void updateDataById(@RequestBody  JSONObject jsonObject) {
        String index = jsonObject.getString("index");
        String type = jsonObject.getString("type");
        String id = jsonObject.getString("id");
        ElasticsearchUtil2.updateDataById(jsonObject,index,type,id);
    }

    @RequestMapping(value = "searchDataById",method = RequestMethod.POST)
    public   Map<String, Object> searchDataById(@RequestBody  JSONObject jsonObject) {
        String index = jsonObject.getString("index");
        String type = jsonObject.getString("type");
        String id = jsonObject.getString("id");
        Map<String, Object> data = ElasticsearchUtil2.searchDataById(index,type,id,null);
        return data;
    }

    @RequestMapping(value = "searchDataPage",method = RequestMethod.POST)
    public EsPage searchDataPage(@RequestParam String index, @RequestParam String type, @RequestParam int startPage, @RequestParam int pageSize, @RequestParam boolean matchPhrase, @RequestParam String searchField, @RequestParam(required =  false)  String sortField, @RequestParam(required =  false)  String highlightField, @RequestParam String searchword, @RequestParam(required =  false)  String viewsField) {
            BoolQueryBuilder boolQuery = QueryBuilders.boolQuery();
            if (matchPhrase == Boolean.TRUE) {
                //不进行分词搜索
                boolQuery.must(QueryBuilders.matchPhraseQuery(searchField, searchword));
            } else {
                boolQuery.must(QueryBuilders.matchQuery(searchField, searchword));
            }
            EsPage esPage = ElasticsearchUtil2.searchDataPage(index, type, startPage, pageSize,boolQuery, viewsField, sortField, highlightField);
            return esPage;
    }

}
